package com.dw.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dw.apis.UserClient;
import com.dw.article.mapper.mp.ArticleInfoMapper;
import com.dw.article.service.ArticleInfoService;
import com.dw.article.service.ArticleMappingService;
import com.dw.article.service.ArticleUserService;
import com.dw.article.service.CommentInfoService;
import com.dw.article.utils.ChangeList;
import com.dw.common.entitys.BaseResponse;
import com.dw.common.utils.ParseClientResultUtils;
import com.dw.model.article.dtos.article.AddArticleDTO;
import com.dw.model.article.dtos.article.QueryArticleDTO;
import com.dw.model.article.dtos.comment.QueryCommentDTO;
import com.dw.model.article.dtos.mapping.AddArticleMappingDTO;
import com.dw.model.article.entitys.ArticleInfo;
import com.dw.model.article.entitys.ArticleMapping;
import com.dw.model.article.entitys.CommentInfo;
import com.dw.model.article.vos.article.GetCountVO;
import com.dw.model.article.vos.article.QueryByIdVO;
import com.dw.model.article.vos.article.QueryUserArticleVO;
import com.dw.model.article.vos.article.SearchArticleVO;
import com.dw.model.article.vos.comment.QueryCommentVO;
import com.dw.model.user.dtos.QueryUserDTO;
import com.dw.model.user.vos.GetUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章信息表(ArticleInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-10-05 20:01:52
 */
@Service("articleInfoService")
public class ArticleInfoServiceImpl extends ServiceImpl<ArticleInfoMapper, ArticleInfo> implements ArticleInfoService {

    @Resource
    private ArticleMappingService articleMappingService;

    @Resource
    private ArticleUserService articleUserService;

    @Resource
    private UserClient userClient;

    @Resource
    private ChangeList changeList;

    /**
     * 添加文章分类标签映射表
     */
    @Override
    public Boolean addMapping(AddArticleDTO addArticleDTO, Long articleId) {
        // 向映射表中添加数据
        addArticleDTO.getCategoryIds().forEach(categoryId -> {
            if(addArticleDTO.getLabelIds().size() == 0){
                AddArticleMappingDTO addArticleMappingDTO = new AddArticleMappingDTO();
                addArticleMappingDTO.setCategoryId(categoryId);
                addArticleMappingDTO.setArticleId(articleId);
                articleMappingService.addMapping(addArticleMappingDTO);
            }
            addArticleDTO.getLabelIds().forEach(labelId -> {
                AddArticleMappingDTO addArticleMappingDTO = new AddArticleMappingDTO();
                addArticleMappingDTO.setLabelId(labelId);
                addArticleMappingDTO.setCategoryId(categoryId);
                addArticleMappingDTO.setArticleId(articleId);
                articleMappingService.addMapping(addArticleMappingDTO);
            });
        });
        return true;
    }

    /**
     * 查询文章列表
     */
    @Override
    public List<SearchArticleVO> queryArticleInfo(List<ArticleInfo> articleInfoList) {

        return articleInfoList.stream().map(item -> {
            Long articleId = item.getLeiId();
            // 根据文章id查询关联的分类和标签
            QueryByIdVO queryByIdVO = articleMappingService.queryById(articleId);
            // 查询文章点赞收藏转发数量
            SearchArticleVO searchArticleVO = new SearchArticleVO();
            GetCountVO articleUserCount = articleUserService.getArticleUserCount(articleId);
            // 拷贝基本数据
            BeanUtils.copyProperties(item, searchArticleVO);
            // 拷贝分类和标签id列表
            List<String> categoryIds = changeList.changeList(queryByIdVO.getCategoryIds());
            searchArticleVO.setCategoryIds(categoryIds);
            List<String> labelIds = changeList.changeList(queryByIdVO.getLabelIds());
            searchArticleVO.setLabelIds(labelIds);
            // 拷贝点赞收藏转发量
            BeanUtils.copyProperties(articleUserCount, searchArticleVO);
            // 获取登录用户与文章之间的点赞，收藏，分享状态
            QueryUserArticleVO queryUserArticleVO = articleUserService.queryUserArticle(articleId);
            searchArticleVO.setIsLiked(queryUserArticleVO.getIsLiked());
            searchArticleVO.setIsStar(queryUserArticleVO.getIsStar());
            searchArticleVO.setIsShared(queryUserArticleVO.getIsShared());
            // openFeign远程调用获取用户信息
            BaseResponse<GetUserVO> getUserVO = userClient.getUserName(item.getCreatedBy());
            // 校验openFeign返回结果
            ParseClientResultUtils.parseClientResult(getUserVO);
            // 设置文章发布者的昵称，头像，个人简介
            searchArticleVO.setNickname(getUserVO.getData().getNickname());
            searchArticleVO.setAvatar(getUserVO.getData().getAvatar());
            searchArticleVO.setIntroduction(getUserVO.getData().getIntroduction());
            // 将雪花算法生成的id转为字符串，便于前端接收
            searchArticleVO.setLeiId(String.valueOf(articleId));
            searchArticleVO.setCreatedBy(String.valueOf(item.getCreatedBy()));
            return searchArticleVO;
        }).collect(Collectors.toList());
    }

    /**
     * 根据文章id列表查询文章信息
     */
    @Override
    public List<ArticleInfo> queryByIds(List<Long> ids) {
        return ids.stream().map(id -> {
            ArticleInfo one = lambdaQuery()
                    .eq(ArticleInfo::getLeiId, id)
                    .one();
            return one;
        }).collect(Collectors.toList());
    }

}

